<template>
	<view class="popup">
		<u-popup :show="show" mode="bottom" round="12" :closeable="true" @close="close">
			<view class="account-main">
				<view class="header u-flex-center">
					<view class="title">选择银行卡</view>
				</view>
				<view class="main-content">
					<scroll-view :scroll-y="true" :style="'height:'+popupHeight+'px'">
						<view class="li u-flex-between" v-for="(item,index) in accountList" :key="index"
							@click="accountChange(index)">
							<view class="li-left u-flex">
								<u-icon :name="item.image" :color="item.iconColor" size="25"></u-icon>
								<view class="li-title">{{item.title}} <text
										v-if="item.subTitle">({{item.subTitle}})</text> </view>
							</view>
							<view class="li-right">
								<u-icon name="/static/icon/account-change.png" size="12" v-if="index == current">
								</u-icon>
							</view>
						</view>
						<view class="li u-flex-between" @click="$navigateTo('/pages/userBankCard/index?type=2')">
							<view class="li-left u-flex">
								<u-icon name="/static/icon/yinhangqia.png" size="25"></u-icon>
								<view class="li-title">添加新银行卡</view>
							</view>
							<view class="li-right">
								<u-icon name="arrow-right" color="#A3A3A3" size="14"></u-icon>
							</view>
						</view>
					</scroll-view>
					<u-button text="确定" :customStyle="submitCustomStyle" @click="confirm">
					</u-button>
					<u-safe-bottom></u-safe-bottom>
				</view>
			</view>
		</u-popup>
	</view>
</template>
<script>
	export default {
		name: "m-account-change",
		data() {
			return {
				userInfo: {},
				current: -1,
				submitCustomStyle: {
					height: "80rpx",
					backgroundColor: "#FF2D71",
					borderRadius: "41rpx",
					fontSize: "31px",
					fontWeight: "bold",
					color: "#FFFFFF",
				},
				pay_type: 1,
				accountList: [],
				popupHeight: 0,
				systemConfig: {},
			};
		},
		props: { // 此处定义传入的参数
			show: {
				type: Boolean,
				default: false,
			},
			type: {
				type: Number,
				default: 1, // 1-充值 2-商品购买
			},
		},
		watch: {},
		mounted() {
			this.userInfo = this.$getUserInfo();
			this.popupHeight = Number(this.$getSystemInfo('windowHeight') * 0.4)
			this.systemConfig = this.$getSystemConfig()
			this.getAccountInfo();
			// 更新银行卡数据后触发
			uni.$on('updateBankCard', () => {
				this.getAccountInfo();
			})
		},
		methods: {
			getAccountInfo() {
				if (this.type == 1) { // 充值
					this.accountList = [{
						id: 0,
						pay_type: 1,
						iconColor: '#0DC919',
						image: 'weixin-circle-fill',
						title: '微信支付',
						is_show: this.systemConfig.recharge_wechat_payment,
					}, {
						id: 0,
						pay_type: 2,
						iconColor: '#1378FE',
						image: 'zhifubao-circle-fill',
						title: '支付宝支付',
						is_show: this.systemConfig.recharge_alipay_payment,
					}];
					// 获取银行卡
					this.getBankCardList(3, this.systemConfig.recharge_bankcard_payment);
				} else { // 支付
					this.accountList = [{
						id: 0,
						pay_type: 1,
						iconColor: '#ffaa00',
						image: 'rmb-circle-fill',
						title: '余额支付',
						subTitle: '余额：￥' + this.$getUserInfo('money'),
						is_show: this.systemConfig.order_balance_payment,
					}, {
						id: 0,
						pay_type: 2,
						iconColor: '#ff5500',
						image: 'rmb-circle-fill',
						title: '赠送余额支付',
						subTitle: '余额：￥' + this.$getUserInfo('give_money'),
						is_show: this.systemConfig.order_give_balance_payment,
					}, {
						id: 0,
						pay_type: 3,
						iconColor: '#0DC919',
						image: 'weixin-circle-fill',
						title: '微信支付',
						is_show: this.systemConfig.order_wechat_payment,
					}, {
						id: 0,
						pay_type: 4,
						iconColor: '#1378FE',
						image: 'zhifubao-circle-fill',
						title: '支付宝支付',
						is_show: this.systemConfig.order_alipay_payment,
					}];
					// 获取银行卡
					this.getBankCardList(5, this.systemConfig.order_bankcard_payment);
				}
			},
			// 获取银行卡账户
			getBankCardList(bank_card_pay_type, is_show) {
				this.$http.get("userbankcard/index").then(res => {
					res.data.bankCardList.forEach((item, index) => {
						item.image = '/static/icon/jh-bank.png';
						item.title = '银行卡';
						item.subTitle = '卡号：' + item.bank_card_account;
						item.pay_type = bank_card_pay_type;
						item.is_show = is_show;
					})
					this.accountList = this.accountList.concat(res.data.bankCardList)
				}).catch(ex => {
					this.$errorToast()
				})
			},
			// 账户选择
			accountChange(index) {
				if (this.accountList[index].is_show != 1) {
					this.$showToast(this.accountList[index].title + '维护中，请稍后重试');
					return;
				}
				this.current = index
				this.$emit('change', this.pay_type);
			},
			// 关闭
			close() {
				// 调用父级重置选择方法
				this.$emit('close');
			},
			// 确认
			confirm() {
				if (this.current == -1) {
					this.$showToast('请选择银行卡');
					return;
				}
				this.$emit('confirm', this.accountList[this.current])
			}
		},
	}
</script>
<style lang="scss">
	.account-main {
		padding: 0 30rpx 30rpx;
		font-size: 28rpx;
		text-align: center;

		.header {
			padding: 30rpx 0;
			position: relative;

			.title {
				font-weight: 600;
			}

			.close {
				position: absolute;
				margin: auto 0;
				top: 0;
				bottom: 0;
				right: 0;
			}
		}

		.main-content {
			padding: 40rpx 20rpx 0;

			.li {
				border-bottom: 1rpx solid #E7E7E7;
				padding: 30rpx 0;

				.li-left {

					.li-title {
						padding-left: 20rpx;

						text {
							padding-left: 10rpx;
							color: #F76E00;
							font-size: 22rpx;
						}
					}
				}
			}

			.li:first-child {
				border-top: 1rpx solid #E7E7E7;
			}
		}
	}
</style>
